﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Fulcrum.UnitTests
{
    [TestClass]
    public class CommandLineSettingTests
    {
        public CommandLineSettingTests()
        {
            // nothing
        }

        CommandLineSetting cmdLineSetting;
        MockSettings settings;

        [TestInitialize]
        public void TestInitialize()
        {
            this.settings = new MockSettings();

            var settingAttribute = new SettingAttribute("AttributeName");
            settingAttribute.Optional = true;
            settingAttribute.Description = "My description";

            var property = GetProperty();

            this.cmdLineSetting = new CommandLineSetting(property, settings, settingAttribute);
        }

        private System.Reflection.PropertyInfo GetProperty()
        {
            var property = this.settings.GetType().GetProperty("MyProperty");
            return property;
        }

        [TestMethod]
        public void TestNameFromAttribute()
        {
            Assert.AreEqual("AttributeName", this.cmdLineSetting.Name);
        }

        [TestMethod]
        public void TestNameFromProperty()
        {
            var property = GetProperty();
            var settingAttribute = new SettingAttribute();

            var setting = new CommandLineSetting(property, this.settings, settingAttribute);

            Assert.AreEqual("MyProperty", setting.Name);
        }

        [TestMethod]
        public void TestDescription()
        {
            Assert.AreEqual("My description", this.cmdLineSetting.Description);
        }

        [TestMethod]
        public void TestOptional()
        {
            Assert.AreEqual(true, this.cmdLineSetting.Optional);
        }

        [TestMethod]
        public void TestHasValueBeforeSet()
        {
            Assert.AreEqual(false, this.cmdLineSetting.HasValue);
        }

        [TestMethod]
        public void TestHasValueAfterSet()
        {
            this.cmdLineSetting.SetValue("323");
            Assert.AreEqual(true, this.cmdLineSetting.HasValue);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidCastException))]
        public void TestSetValueNull()
        {
            this.cmdLineSetting.SetValue(null);

            Assert.Fail("An exception was expected");
        }

        [TestMethod]
        [ExpectedException(typeof(FormatException))]
        public void TestSetValueEmpty()
        {
            this.cmdLineSetting.SetValue("");

            Assert.Fail("An exception was expected");
        }

        [TestMethod]
        public void TestSetValue()
        {
            this.cmdLineSetting.SetValue("235432");

            Assert.AreEqual(235432, this.settings.MyProperty);
        }

        class MockSettings : ISettings
        {
            public MockSettings()
            {
                // nothing
            }

            public int MyProperty
            {
                get;
                private set;
            }

            public void ValidateSettings()
            {
                throw new NotImplementedException();
            }

        }
    }
}
